package org.loon.game.sample.llk;

import java.util.LinkedList;

import org.loon.framework.javase.game.core.graphics.opengl.LTexture;

public class PathFinder {
	
	private LinkedList<pGrid>[] path;
	pGrid[][] grid; 
	int xBound,yBound;
	
	public PathFinder(pGrid[][] grid , int x,int y){
		path = new LinkedList[3];
		path[0] = new LinkedList<pGrid>();
		path[1] = new LinkedList<pGrid>();
		path[2] = new LinkedList<pGrid>();
		
		this.grid = grid;
		xBound = x;
		yBound = y;
		
	}
	
	private boolean xdirect(pGrid start, pGrid end, LinkedList<pGrid> path) {
		if (start.getYpos() != end.getYpos())
			return false;
		int direct = 1;
		if (start.getXpos() > end.getXpos()) {
			direct = -1;
		}
		path.clear();
		for (int x = start.getXpos() + direct; x != end.getXpos() && x < yBound
				&& x >= 0; x += direct) {
			if (grid[start.getYpos()][x].isVisible()) {
				return false;
			}
			path.add(grid[start.getYpos()][x]);
		}

		path.add(end);
		return true;
	}

	private boolean ydirect(pGrid start, pGrid end, LinkedList<pGrid> path) {
		if (start.getXpos() != end.getXpos()) {
			return false;
		}
		int direct = 1;
		if (start.getYpos() > end.getYpos()) {
			direct = -1;
		}
		path.clear();
		for (int y = start.getYpos() + direct; y != end.getYpos() && y < xBound
				&& y >= 0; y += direct) {
			if (grid[y][start.getXpos()].isVisible()) {
				return false;
			}
			path.add(grid[y][start.getXpos()]);
		}

		path.add(end);
		return true;
	}

	public int findPath(pGrid start, pGrid end) {
		if (xdirect(start, end, path[0])) {
			return 1;
		}
		if (ydirect(start, end, path[0])) {
			return 1;
		}
		pGrid xy = grid[start.getYpos()][end.getXpos()];
		if (!xy.isVisible() && xdirect(start, xy, path[0])
				&& ydirect(xy, end, path[1])) {
			return 2;
		}
		pGrid yx = grid[end.getYpos()][start.getXpos()];
		if (!yx.isVisible() && ydirect(start, yx, path[0])
				&& xdirect(yx, end, path[1])) {
			return 2;
		}
		path[0].clear();
		for (int y = start.getYpos() - 1; y >= 0; y--) {
			xy = grid[y][start.getXpos()];
			yx = grid[y][end.getXpos()];
			if (xy.isVisible()) {
				break;
			}
			path[0].add(xy);
			if (!yx.isVisible() && xdirect(xy, yx, path[1])
					&& ydirect(yx, end, path[2])) {
				return 3;
			}
		}

		path[0].clear();
		for (int y = start.getYpos() + 1; y < xBound; y++) {
			xy = grid[y][start.getXpos()];
			yx = grid[y][end.getXpos()];
			if (xy.isVisible()) {
				break;
			}
			path[0].add(xy);
			if (!yx.isVisible() && xdirect(xy, yx, path[1])
					&& ydirect(yx, end, path[2])) {
				return 3;
			}
		}

		path[0].clear();
		for (int x = start.getXpos() - 1; x >= 0; x--) {
			yx = grid[start.getYpos()][x];
			xy = grid[end.getYpos()][x];
			if (yx.isVisible()) {
				break;
			}
			path[0].add(yx);
			if (!xy.isVisible() && ydirect(yx, xy, path[1])
					&& xdirect(xy, end, path[2])) {
				return 3;
			}
		}

		path[0].clear();
		for (int x = start.getXpos() + 1; x < yBound; x++) {
			yx = grid[start.getYpos()][x];
			xy = grid[end.getYpos()][x];
			if (yx.isVisible()) {
				break;
			}
			path[0].add(yx);
			if (!xy.isVisible() && ydirect(yx, xy, path[1])
					&& xdirect(xy, end, path[2])) {
				return 3;
			}
		}

		return 0;
	}
	
	public boolean needResh() {
		//nexts = null;
		//nexte = null;
		/*for (int sy = 1; sy < yBound - 1; sy++) {
			for (int sx = 1; sx < xBound - 1; sx++)
				if (grid[sy][sx].isVisible()) {
					for (int ey = sy; ey < yBound - 1; ey++) {
						for (int ex = 1; ex < xBound - 1; ex++){
							System.out.println("grid[ey][ex].isVisible()="+grid[ey][ex].isVisible());
							System.out.println(" (ey != sy || ex != sx)="+ (ey != sy || ex != sx));
							System.out.println("grid[sy][sx].type.equals(grid[ey][ex].type)="+grid[sy][sx].type.equals(grid[ey][ex].type));
							if (grid[ey][ex].isVisible()
									&& (ey != sy || ex != sx)
									&& grid[sy][sx].type.equals(grid[ey][ex].type)) {
								int pcount = findPath(grid[sy][sx], grid[ey][ex]);		
								System.out.println("pcount="+pcount);
								if (pcount != 0) {
									return false;
								}
							}
						}
					}
				}
		}*/
		
		for (int sy = 1; sy < yBound - 1; sy++) {
			for (int sx = 1; sx < xBound - 1; sx++)
				if (grid[sx][sy].isVisible()) {
					for (int ey = sy; ey < yBound - 1; ey++) {
						for (int ex = 1; ex < xBound - 1; ex++){
							//System.out.println("grid[ey][ex].isVisible()="+grid[ey][ex].isVisible());
							//System.out.println(" (ey != sy || ex != sx)="+ (ey != sy || ex != sx));
							//System.out.println("grid[sy][sx].type.equals(grid[ey][ex].type)="+grid[sy][sx].type.equals(grid[ey][ex].type));
							if (grid[ex][ey].isVisible()
									&& (ey != sy || ex != sx)
									&& grid[sx][sy].type.equals(grid[ex][ey].type)) {
								int pcount = findPath(grid[sx][sy], grid[ex][ey]);		
								System.out.println("pcount="+pcount);
								if (pcount != 0) {
									return false;
								}
							}
						}
					}
				}
		}
		
		return true;
	}
	
}
